import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import {formatMessage} from './intl';
import * as dto from './DTO';
import stringify from 'Shared/utils/stringify';

const baseUrl = '/afterSales/api/v1';

const getListDesc = formatMessage({
    id: 'api.getList',
    defaultMessage: '查询大区'
});

const getDetailDesc = formatMessage({
    id: 'api.getDetail',
    defaultMessage: '查询大区详情'
});

const modifyDesc = formatMessage({
    id: 'api.modify',
    defaultMessage: '大区关系维护'
});

const getDealerDesc = formatMessage({
    id: 'api.message.getDealerDesc',
    defaultMessage: '查询总代信息'
});

const getListTreeDesc = formatMessage({
    id: 'api.getListTreeDesc',
    defaultMessage: '查询大区树'
});

const getRegionDesc = formatMessage({
    id: 'api.getRegionDesc',
    defaultMessage: '查询国际区域列表'
});

// 查询大区数据详情
export const getDetail = id => request(`${baseUrl}/marketDepartments/forDealer/${id}`, null, dto.Detail)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));

// 关系维护
export const modify = (id, data) => request(`${baseUrl}/marketDepartments/forDealer/${id}`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, modifyDesc));

// 查询大区数据列表
export const getList = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/marketDepartments?${queryString}`, null, dto.List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};

// 查询经销商
export const getDealers = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dealers/forSubDealer?${queryString}`, null, dto.DealerSelectList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDealerDesc));
};

// 根据国际区域查询大区
export const getMarketDepartments = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/marketDepartments/forSubDealer?${queryString}`, null, dto.MarketDepartmentList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListTreeDesc));
};

// 查询国际区域列表
export const getRegions = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/areaTileds/forSubDealer?${queryString}`, null, dto.RegionList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getRegionDesc));
};

